package com.rytong.tools.ui.chart;

import java.util.ArrayList;
import java.util.List;

import com.rytong.tools.ui.Component;
import com.rytong.tools.ui.CssStyle;
import com.rytong.tools.ui.LPLayout;
import com.rytong.tools.ui.LPScrollView;
import com.rytong.tools.ui.LPLayout.MyLPLayout;
import com.rytong.tools.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.Paint.Style;
import android.graphics.RadialGradient;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsoluteLayout;

/**
 * @author lixiao 气泡控件
 */
@SuppressWarnings("deprecation")
public class LPBallPlotLayout extends Component implements Chart {

    // x轴的marginTop
    float xScaleMaginTop_;
    // y轴的marginTop
    float yScaleMaginTop_;
    // y轴的刻度值集合
    List<String> yValueSpace_;
    //x轴的刻度值集合
    List<String> xValueSpace_;
    
    // 气泡视图
    public class LpBall extends View {

        Component component_;
        // 画笔
        Paint paint_;
        // 样式
        CssStyle cssStyle_;
        // 气泡的半径
        float ballRadius_;
        // 气泡的颜色
        int ballColor_;
        // 点击事件
        String onClickValue_;
        // 渐变色属性
        int gradientType_;
        int startColorStr_;
        int endColorStr_;
        // 渐变属性
        RadialGradient radialGradient_;
        // 记录是第几个气泡(从0开始)
        int number_;

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawCircle(ballRadius_, ballRadius_, ballRadius_, paint_);
            if (startColorStr_ != 0 && endColorStr_ != 0) {
                radialGradient_ = new RadialGradient(ballRadius_ * 3 / 4, ballRadius_ * 3 / 4, ballRadius_ / 2,
                        startColorStr_, endColorStr_, Shader.TileMode.CLAMP);
                paint_.setShader(radialGradient_);
            }
            canvas.drawCircle(ballRadius_ * 3 / 4, ballRadius_ * 3 / 4, ballRadius_ / 2, paint_);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            super.onTouchEvent(event);
            int touchEvent = event.getAction();
            switch (touchEvent) {
            case MotionEvent.ACTION_DOWN:
            	// 按下tr时候变色。
            	trDownColor(tr_, llvSV_);
                break;
            case MotionEvent.ACTION_UP:
            	// 抬起tr恢复颜色。
            	trUpColor(tr_, llvSV_);
                onClick(onClickValue_);
                break;
            default:

            }
            return true;
        }

        public LpBall(Context context) {
            super(context);
        }

        public LpBall(Context context, Component c, int number) {
            super(context);
            component_ = c;
            number_ = number;
        }

        public void mould() throws Exception {
            mouldChildList();
            // 获得点击时的lua事件
            onClickValue_ = (String) component_.property_.get("onclick");
            cssStyle_ = component_.cssStyle();
            // 获得气泡的半径
            ballRadius_ = component_.width_ / 2;
            // 获取气泡的颜色
            String color = (String) cssStyle_.getProperty(CssStyle.BACKGROUNDCOLOR);
            if (null != color && !color.equals("")) {
                ballColor_ = Utils.getColorValue(color);
            }
            // 设置画笔
            paint_ = new Paint();
            paint_.setAntiAlias(true);
            paint_.setStyle(Style.FILL);
            paint_.setColor(ballColor_);
            // 获得颜色渐变相关属性
            gradientType_ = cssStyle_.gradientType;
            startColorStr_ = cssStyle_.startColorStr;
            endColorStr_ = cssStyle_.endColorStr;
            // 获取气泡的value
            String compontValue = (String) component_.getPropertyByName("value");

            android.widget.AbsoluteLayout.LayoutParams lp = null;
            if (component_.left_ > 0) {
                //如果服务端没给height，则根据value计算
                if(component_.height_ <= 0){
                    if (yValueSpace_ == null)
                        return;
                    int valueSize = yValueSpace_.size();
                    //气泡中心的高度
                    component_.height_ =(int) ((valueSize - 1) * yScaleMaginTop_ * Float.parseFloat(compontValue) / (Float
                            .parseFloat(yValueSpace_.get(0)) - Float.parseFloat(yValueSpace_.get(valueSize - 1))));
                }
                //计算气泡的top值
                if(component_.top_ <= 0 ){
                    component_.top_ = component_.height_;
                }
                lp = new android.widget.AbsoluteLayout.LayoutParams(component_.width_, component_.height_,
                        component_.left_, LPBallPlotLayout.this.height_ - component_.top_);
            } else {
                if (yValueSpace_ == null)
                    return;
                int valueSize = yValueSpace_.size();
                // 获取气泡的高度
                int ballHeight = (int) ((valueSize - 1) * yScaleMaginTop_ * Float.parseFloat(compontValue) / (Float
                        .parseFloat(yValueSpace_.get(0)) - Float.parseFloat(yValueSpace_.get(valueSize - 1))));
                lp = new android.widget.AbsoluteLayout.LayoutParams(component_.width_, component_.height_,
                        (int) (xScaleMaginTop_ * (number_ + 0.5) - (component_.width_ / 2.0f)),
                        (int) (LPBallPlotLayout.this.height_ - ballHeight - (component_.width_ / 2.0f)));
                component_.left_ = (int) (xScaleMaginTop_ * (number_ + 0.5) - (component_.width_ / 2.0f));
                component_.top_ = (int) (LPBallPlotLayout.this.height_ - ballHeight - (component_.width_ / 2.0f));
            }
            setLayoutParams(lp);
        }

    }

    public class MyLPBallPlotLayout extends AbsoluteLayout implements CompositedComponent {

        public MyLPBallPlotLayout(Context context, String value) {
            super(context);
        }

        @Override
        public Component composited() {
            return LPBallPlotLayout.this;
        }

    }

    @Override
    public void initRealView(Activity activity, String nodeValue) {
        super.initRealView(activity, nodeValue);
        realView_ = new MyLPBallPlotLayout(activity, nodeValue);
        childrenList_ = new ArrayList<Component>();
    }

    @Override
    public void mouldW() throws Exception {
        int size = childrenList_.size();
        AbsoluteLayout layout = (AbsoluteLayout) realView_;
        layout.removeAllViews();
        Component child = null;
        for (int i = 0; i < size; i++) {
            child = (Component) childrenList_.get(i);
            LpBall lpBall = new LpBall(getContext(), child, i);
            lpBall.mould();
            layout.addView(lpBall);
        }
    }
    
    @Override
    public void setXScaleMarginTop(float valueSpace) {
        xScaleMaginTop_ = valueSpace;
    }

    @Override
    public float getXScalemarginTop() {
        return xScaleMaginTop_;
    }

    @Override
    public void setYScaleMarginTop(float valueSpace) {
        yScaleMaginTop_ = valueSpace;
    }

    @Override
    public float getYScaleMarginTop() {
        return yScaleMaginTop_;
    }

    @Override
    public void setYScaleValues(List<String> values) {
        yValueSpace_ = values;
    }

    @Override
    public List<String> getYScaleVaues() {
        return yValueSpace_;
    }

    @Override
    public void setXScaleValues(List<String> values) {
        xValueSpace_ = values;
    }

    @Override
    public List<String> getXScaleValue() {
        return xValueSpace_;
    }

    @Override
    public void releaseResource(Component component) {
    	super.releaseResource(component);
    	if(xValueSpace_ != null){
    		xValueSpace_.clear();
    		xValueSpace_ = null;
    	}
    	if(yValueSpace_ != null){
    		yValueSpace_.clear();
    		yValueSpace_ = null;
    	}
    }
}
